﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Control : MonoBehaviour {

    //目标点坐标
    private Vector3 mTargetPos;
    private void Update()
    {
        //按下鼠标右键的时候
        if (Input.GetMouseButton(1))
        {
            //获取屏幕坐标
            Vector3 mScreenPos = Input.mousePosition;
            //定义射线
            Ray mRay = Camera.main.ScreenPointToRay(mScreenPos);
            RaycastHit mHit;
            //判断射线是否击中地面
            if (Physics.Raycast(mRay,out mHit))
            {
                if (mHit.collider.gameObject.tag=="Terrain")
                {
                    //获取目标坐标
                    mTargetPos = mHit.point;
                    //让主角面朝目标坐标并向目标移动
                    transform.LookAt(mTargetPos);
                    //播放奔跑动画
                    transform.gameObject.GetComponent<Animation>().Play("run");
                    transform.Translate(Vector3.forward * 0.5F);
                }
            }
        }
        //松开鼠标右键时
        if (Input.GetMouseButtonUp(1))
        {
            transform.gameObject.GetComponent<Animation>().Play("idle");
        }
    }
}
